package operator;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.operators.StreamFilter;

/**
 * Flink的Filter 底层使用
 */
public class FilterDemo1 {

    public static void main(String[] args) throws Exception {
        // Flink配置
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 80);
        // 创建带Webui的Flink本地执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        // 使用socket创建DataStream
        DataStreamSource<String> data = env.socketTextStream("192.168.56.10", 8888);

        FilterFunction<String> filterFunction = new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                int i = Integer.parseInt(s);
                return i % 2 == 0;
            }
        };

        // 调用transform
        SingleOutputStreamOperator<String> filtered = data.transform("MyMapFilter", TypeInformation.of(String.class), new StreamFilter<String>(filterFunction));

        // sink
        filtered.print();

        env.execute("map operator");
    }


}
